home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 January / macpower199701.bin / AMUG / Programming_10 / WASTE 1.3a1.sit / WASTE 1.3a1 Distribution / WASTE 1.3a1 / WEUtilities.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-23  |  3.2 KB  |  173 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEUtilities.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Miscellaneous General-Purpose Utilities
  6.  *
  7.  *  Copyright (c) 1993-1996 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. pascal Boolean _WEBlockCmp(const void *block1, const void *block2, register Size blockSize)
  18. {
  19.     register const char *p1 = (const char *) block1;
  20.     register const char *p2 = (const char *) block2;
  21.  
  22.     while ( --blockSize >= 0 )
  23.         if ( *p1++ != *p2++ )
  24.             return false;
  25.  
  26.     return true;
  27. }
  28.  
  29. pascal void _WEBlockClr(void *block, register Size blockSize)
  30. {
  31.     register char *p = (char *) block;
  32.  
  33.     while ( --blockSize >= 0 )
  34.         *p++ = 0;
  35. }
  36.  
  37. pascal void _WEForgetHandle(Handle *h)
  38. {
  39.     Handle theHandle;
  40.  
  41.     if ((theHandle = *h) != nil)
  42.     {
  43.         *h = nil;
  44.         DisposeHandle(theHandle);
  45.     }
  46. }
  47.  
  48. pascal Boolean _WESetHandleLock(Handle h, Boolean lock)
  49. {
  50.     Boolean oldLock = (HGetState(h) & (1 << 7)) != 0;
  51.  
  52.     if (lock != oldLock)
  53.         if (lock)
  54.             HLock(h);
  55.         else
  56.             HUnlock(h);
  57.  
  58.     return oldLock;
  59. }
  60.  
  61. pascal void _WEReorder(SInt32 *a, SInt32 *b)
  62. {
  63.     if (*a > *b)
  64.     {
  65.         SInt32 temp = *a;
  66.         *a = *b;
  67.         *b = temp;
  68.     }
  69. }
  70.  
  71. pascal OSErr _WEAllocate(Size blockSize, UInt32 allocFlags, Handle *h)
  72. {
  73.     // Allocate a new relocatable block.
  74.     // AllocFlags may specify whether the block should be cleared and whether
  75.     // temporary memory should be used.
  76.  
  77.     Handle theHandle = nil;
  78.     OSErr err;
  79.  
  80.     // if kAllocTemp is specified, try tapping temporary memory
  81.     if (allocFlags & kAllocTemp)
  82.     {
  83.         theHandle = TempNewHandle(blockSize, &err);
  84.     }
  85.  
  86.     // if kAllocTemp isn't specified, or TempNewHandle failed, try with current heap
  87.     if (theHandle == nil)
  88.     {
  89.         theHandle = NewHandle(blockSize);
  90.         err = MemError();
  91.     }
  92.  
  93.     // if kAllocClear is specified, zero the block
  94.     if ((allocFlags & kAllocClear) && (theHandle != nil))
  95.     {
  96.         _WEBlockClr(*theHandle, blockSize);
  97.     }
  98.  
  99.     *h = theHandle;
  100.     return err;
  101. }
  102.  
  103. pascal OSErr _WESplice ( Handle h, const void * blockPtr, SInt32 blockSize, SInt32 offset )
  104. {
  105.     SInt32 oldSize, newSize ;
  106.     OSErr err ;
  107.  
  108.     //    get handle size
  109.     oldSize = GetHandleSize ( h ) ;
  110.  
  111.     //    calculate new size
  112.     newSize = oldSize + blockSize ;
  113.  
  114.     if ( blockSize < 0 )
  115.     {
  116.         //    negative blockSize means we must remove a portion of the block
  117.         //    offset == -1 means remove trailing portion
  118.         if ( offset == -1 )
  119.         {
  120.             offset = newSize ;
  121.         }
  122.  
  123.         //    sanity checks
  124.         err = paramErr ;
  125.         if ( ( offset < 0 ) || ( offset > newSize ) )
  126.         {
  127.             goto cleanup ;
  128.         }
  129.  
  130.         //    compact the handle
  131.         BlockMoveData ( * h + ( offset - blockSize ), * h + offset, newSize - offset ) ;
  132.     }
  133.  
  134.     //    resize the handle
  135.     SetHandleSize ( h, newSize ) ;
  136.     if ( ( err = MemError ( ) ) != noErr )
  137.     {
  138.         goto cleanup ;
  139.     }
  140.  
  141.     if ( blockSize > 0 )
  142.     {
  143.         //    positive blockSize means make room within the handle
  144.         //    offset == -1 means append new portion at the end
  145.         if ( offset == -1 )
  146.         {
  147.             offset = oldSize ;
  148.         }
  149.  
  150.         //    sanity checks
  151.         err = paramErr ;
  152.         if ( ( offset < 0 ) || ( offset > oldSize ) )
  153.         {
  154.             goto cleanup ;
  155.         }
  156.  
  157.         //    make some room at offset
  158.         BlockMoveData ( * h + offset, * h + offset + blockSize, oldSize - offset ) ;
  159.  
  160.         //    insert blockPtr into gap (unless blockPtr == nil)
  161.         if ( blockPtr != nil )
  162.         {
  163.             BlockMoveData ( blockPtr, * h + offset, blockSize ) ;
  164.         }
  165.     }
  166.  
  167.     //    clear result code
  168.     err = noErr ;
  169.  
  170. cleanup :
  171.     return err ;
  172. }
  173.